package com.kk.demo.java8.stream.base01;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 并发容器对象
 * @author liujun
 * @date 2016年8月28日
 * @verion 0.0.1
 */
public class StreamCollector {

    public StreamCollector() {
    }

    public static void main(String[] args) {
        StreamCollector stream1 = new StreamCollector();
        List<Parson> list = stream1.newList();

        stream1.toList(list);
        stream1.group(list);
        stream1.avg(list);
    }

    public List<Parson> newList() {
        return Arrays.asList(new Parson("a1", 1), new Parson("b1", 100), new Parson("c3", 200), new Parson("P4", 500));
    }

    /**
     * 转化为集体对象
     * @param param
     */
    public void toList(List<Parson> param) {
        List<Parson> list = param.stream().filter(s -> s.getName().contains("1")).collect(Collectors.toList());

        System.out.println("当前的parson对象:" + list);
    }

    /**
     * 进行分组操作
     * @param param
     */
    public void group(List<Parson> param) {
        Map<String, List<Parson>> map = param.stream().collect(Collectors.groupingBy(s -> s.getName().substring(1)));

        System.out.println("分组的结果:" + map);
    }

    public void avg(List<Parson> param) {
        double value = param.stream().collect(Collectors.averagingInt(s -> s.getValue()));
        System.out.println("当前的平均值为:" + value);
    }
}
